package com.hourse.controller;

import com.hourse.pojo.HouseSearch;
import com.hourse.service.HouseSeachService;
import com.hourse.util.PageUtil;
import com.hourse.util.Result;
import com.hourse.util.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/housesearch")
@CrossOrigin
public class HouseController {
    @Autowired
    private HouseSeachService houseSeachService;
    @RequestMapping(method = RequestMethod.POST)
   public Result save(@RequestBody HouseSearch search){
       houseSeachService.save(search);
       return new Result(true, StatusCode.OK,"索引添加成功");
   }

/*   @RequestMapping(value = {"/{key}/{price}/{kaipan}/{wuye}/{status}/{tese}/{type}/{page}/{size}","/{key}/{price}/{kaipan}/{wuye}/{status}/{tese}/{page}/{size}",
           "/{key}/{price}/{kaipan}/{wuye}/{status}/{page}/{size}","/{key}/{price}/{kaipan}/{wuye}/{page}/{size}","/{key}/{price}/{kaipan}/{page}/{size}",
           "/{key}/{price}/{page}/{size}","/{key}/{page}/{size}","/{page}/{size}",
    },method = RequestMethod.GET)
    public Result findBykey(@PathVariable(value = "key",required = false) String key,@PathVariable(value = "price",required = false) Integer price,@PathVariable(value = "kaipan",required = false) Integer kaipan,
                            @PathVariable(value = "wuye",required = false) Integer wuye,@PathVariable(value = "status",required = false) Integer status,@PathVariable(value = "tese",required = false) Integer tese,
                            @PathVariable(value = "type",required = false) Integer type,
                            @PathVariable Integer page,@PathVariable Integer size){
       Page<HouseSearch> searchPage= houseSeachService.findBykey(key,price,kaipan,wuye,status,tese,type,page,size);
        return new Result(true,StatusCode.OK,"查询成功",new PageUtil<HouseSearch>(page,searchPage.getTotalElements(),searchPage.getContent(),size));
    }*/
    @GetMapping("/search")
    public Result findBykey(@RequestParam(value = "page",required = false,defaultValue = "1")Integer page,@RequestParam(value = "size",defaultValue = "10",required = false)Integer size,
                            @RequestParam(value = "key",defaultValue = "",required = false)String key,@RequestParam(value = "startprice",required = false)Integer startprice,@RequestParam(value = "endprice",required = false)Integer endprice,@RequestParam(value = "kaipan",required = false)Integer kaipan,
                            @RequestParam(value = "wuye",required = false)Integer wuye,@RequestParam(value = "status",required = false)Integer status,
                            @RequestParam(value = "tese",required = false)Integer tese,@RequestParam(value = "type",required = false)Integer type,
                            @RequestParam(value = "ISDesc",required = false)Integer isdesc){
        Page<HouseSearch> searchPage= houseSeachService.findBykey(key,startprice,kaipan,wuye,status,tese,type,page,size,endprice,isdesc);
        return new Result(true,StatusCode.OK,"查询成功",new PageUtil<HouseSearch>(page,searchPage.getTotalElements(),searchPage.getContent(),size));
    }
    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    public Result delkey(@PathVariable long id){
        houseSeachService.delkey(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

}
